package com.dindang.ddm.mvp.presenter;

import android.app.Application;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.dindang.ddm.app.AppConst;
import com.dindang.ddm.mvp.contract.MeContract;
import com.dindang.ddm.mvp.ui.activity.LoginActivity;
import com.dindang.ddm.mvp.ui.activity.MainActivity;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;
import com.longxi.gamer_base.BaseAppConst;
import com.longxi.gamer_base.model.LoginEvent;
import com.longxi.gamer_base.model.User;
import com.longxi.gamer_base.utils.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;


@FragmentScope
public class MePresenter extends BasePresenter<MeContract.Model, MeContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    private boolean isFirst = true;
    String[] mRoleType = {"系统管理员", "维修工", "客服"};

    @Inject
    public MePresenter(MeContract.Model model, MeContract.View rootView) {
        super(model, rootView);
    }

    public void initDate(){
        if(!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        if(isFirst){
            LogUtils.debugInfo("Me页面重新加载");
            isFirst = false;
            setAccountUi();
        }
    }

    /**
     * 设置用户UI界面信息
     */
    public void setAccountUi() {
        String userInfo = SharedPreferencesUtils.getParam(mApplication, AppConst.KEY_ACCOUNT_INFO, "").toString();
        if(!TextUtils.isEmpty(userInfo)){
            BaseAppConst.accountInfo = JSON.parseObject(userInfo, User.AccountInfo.class);
        }
        if(null != BaseAppConst.accountInfo){
            mRootView.getTvCompanyName().setText(BaseAppConst.accountInfo.getCorpName());
            mRootView.getTvCustName().setText(BaseAppConst.accountInfo.getUserName());
            mRootView.getTvType().setVisibility(View.VISIBLE);
            mRootView.getLlLogin().setVisibility(View.VISIBLE);
            if("1".equals(BaseAppConst.accountInfo.getIsAdmin())){
                mRootView.getTvType().setText("超级管理员");
            }else{
                String str = BaseAppConst.accountInfo.getRoleType();
                try {
                    int a = Integer.parseInt(str);
                    mRootView.getTvType().setText(mRoleType[a]);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        }else{
            mRootView.getTvCompanyName().setText("请先登录");
            mRootView.getTvCustName().setText("");
            mRootView.getTvType().setVisibility(View.GONE);
            mRootView.getLlLogin().setVisibility(View.GONE);
        }
    }

    public void initView() {
        mRootView.getToolbarTitle().setText("我的");
        initListener();
    }

    private void initListener() {
        mRootView.getClLogin().setOnClickListener(v -> {
            if(BaseAppConst.accountInfo==null){
                ARouter.getInstance().build("/activity/LoginActivity").navigation(mAppManager.getCurrentActivity());
            }
        });
        mRootView.getTvLoginOut().setOnClickListener(v -> {
            loginOut();
        });
    }

    public void loginOut() {
        BaseAppConst.accountInfo=null;
        SharedPreferencesUtils.setParam(mApplication, AppConst.KEY_ACCOUNT_INFO, "");
        ArmsUtils.makeText(mRootView.getFragment().getActivity(),"退出成功");
//        ARouter.getInstance().build("/activity/LoginActivity").navigation(mAppManager.getCurrentActivity());
//       mRootView.getFragment().getActivity().finish();

        mAppManager.killActivity(MainActivity.class);
        mAppManager.startActivity(LoginActivity.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;

        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loginSuccess(LoginEvent loginEvent){
        if(!isFirst) {
            isFirst = true;
            initDate();
        }
    }
}
